package com.ruoyi.human.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.human.domain.HumanClinicalTrial;
import com.ruoyi.human.domain.Participant;
import com.ruoyi.human.service.IHumanClinicalTrialService;
import com.ruoyi.human.service.IParticipantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.human.domain.InformedConsent;
import com.ruoyi.human.service.IInformedConsentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 知情同意Controller
 * 
 * @author gw
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/consent")
public class InformedConsentController extends BaseController
{
    @Autowired
    private IInformedConsentService informedConsentService;
    @Autowired
    private IParticipantService participantService;
    @Autowired
    private IHumanClinicalTrialService humanClinicalTrialService;
    /**
     * 查询知情同意列表
     */
    @RequiresPermissions("human:consent:list")
    @GetMapping("/list")
    public TableDataInfo list(InformedConsent informedConsent)
    {
        startPage();
        List<InformedConsent> list = informedConsentService.selectInformedConsentList(informedConsent);
        for (InformedConsent informedConsent1:list){
            Long trialId=informedConsent1.getTrialId();
            HumanClinicalTrial humanClinicalTrial=humanClinicalTrialService.selectHumanClinicalTrialByTrialId(trialId);
            if(humanClinicalTrial!=null){
                informedConsent1.setTrialName(humanClinicalTrial.getTrialName());
            }
        }
        for (InformedConsent informedConsent1:list){
            Long participantId=informedConsent1.getParticipantId();
            Participant participant=participantService.selectParticipantByParticipantId(participantId);
            if(participant!=null){
                informedConsent1.setParticipantName(participant.getParticipantName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出知情同意列表
     */
    @RequiresPermissions("human:consent:export")
    @Log(title = "知情同意", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InformedConsent informedConsent)
    {
        List<InformedConsent> list = informedConsentService.selectInformedConsentList(informedConsent);
        ExcelUtil<InformedConsent> util = new ExcelUtil<InformedConsent>(InformedConsent.class);
        util.exportExcel(response, list, "知情同意数据");
    }

    /**
     * 获取知情同意详细信息
     */
    @RequiresPermissions("human:consent:query")
    @GetMapping(value = "/{consentId}")
    public AjaxResult getInfo(@PathVariable("consentId") Long consentId)
    {
        return success(informedConsentService.selectInformedConsentByConsentId(consentId));
    }

    /**
     * 新增知情同意
     */
    @RequiresPermissions("human:consent:add")
    @Log(title = "知情同意", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InformedConsent informedConsent)
    {
        informedConsent.setCreatedAt(new Date());
        informedConsent.setCreatedBy(SecurityUtils.getUsername());
        informedConsentService.insertInformedConsent(informedConsent);
        Participant participant=participantService.selectParticipantByParticipantId(informedConsent.getParticipantId());
        if (participant!=null){
            participant.setConsentStatus(informedConsent.getConsentStatus());
            participantService.updateParticipant(participant);
        }
        return toAjax(true);
    }

    /**
     * 修改知情同意
     */
    @RequiresPermissions("human:consent:edit")
    @Log(title = "知情同意", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InformedConsent informedConsent)
    {
        informedConsent.setUpdatedBy(SecurityUtils.getUsername());
        informedConsent.setUpdatedAt(new Date());
        informedConsent.setVersion(informedConsent.getVersion()+1);
        informedConsentService.updateInformedConsent(informedConsent);
        Participant participant=participantService.selectParticipantByParticipantId(informedConsent.getParticipantId());
        if (participant!=null){
            participant.setConsentStatus(informedConsent.getConsentStatus());
            participantService.updateParticipant(participant);
        }
        return toAjax(true);
    }

    /**
     * 删除知情同意
     */
    @RequiresPermissions("human:consent:remove")
    @Log(title = "知情同意", businessType = BusinessType.DELETE)
	@DeleteMapping("/{consentIds}")
    public AjaxResult remove(@PathVariable Long[] consentIds)
    {
        for (Long id:consentIds){
            InformedConsent informedConsent=informedConsentService.selectInformedConsentByConsentId(id);
            informedConsent.setDeletedAt(new Date());
            informedConsent.setDeletedBy(SecurityUtils.getUsername());
            informedConsent.setDeletedFlag(1);
            informedConsentService.updateInformedConsent(informedConsent);
        }
        return toAjax(true);
    }


}
